package com.example.layui.controller;


import com.example.layui.common.ResponseServer;
import com.example.layui.pojo.UserInfo;
import com.example.layui.util.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(){
        return "index";
    }
    @RequestMapping("/table")
    public String table(){
        return "table";
    }


    @RequestMapping("/queryUserInfo")
    @ResponseBody
    public ResponseServer queryUserInfo(String pageNo, String pageNumber, String name){
        if(pageNo==null || pageNo.equals("")|| pageNo.equals("null")
                || !pageNo.matches("\\d+")){
            pageNo = null;
        }
        if(pageNumber==null || pageNumber.equals("")|| pageNumber.equals("null")
                || !pageNumber.matches("\\d+")){
            pageNumber = null;
        }
        if(name==null || name.equals("")|| name.equals("null")){
            name = null;
        }
        List<UserInfo> userInfos = DBUtils.queryUserInfo(parseInt(pageNo,null),parseInt(pageNumber,null),name);
        return ResponseServer.getResponse(0,200,"",userInfos);
    }


    public Integer parseInt(String val,Integer def){
        if(val==null || val.equals("") || !val.matches("\\d+")){
            return def;
        }else{
            try {
                return Integer.parseInt(val);
            }catch (Exception e){

            }finally {
                return def;
            }
        }
    }


}
